//
//  TTActionSheet.m
//  WelfareManager
//
//  Created by timtian on 17/1/4.
//  Copyright © 2017年 timtian. All rights reserved.
//

#import "TTActionSheet.h"

#define kCollectionViewHeight 178.0f
#define kSubTitleHeight 65.0f
#define kSheetCellHeight 55.0f
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kCellSpacing 10.0f

@class TTActionSheet;
@interface TTActionSheet()<UIGestureRecognizerDelegate>

@property (nonatomic, copy  )      NSString  *cancelStr;
@property (nonatomic, strong)      NSArray   *titles;
@property (nonatomic, weak  )      UIView    *buttomView;
@property (nonatomic, copy  )      NSString  *attachTitle;
@property (nonatomic        )      CGFloat   actionSheetHeight;
@property(nonatomic, assign) BOOL clearDismiss;

@end
@implementation TTActionSheet

- (void)dealloc {
    NSLog(@"TTActionSheet----dealloc");
}

#pragma mark-- public
- (instancetype) initWithCancelStr:(NSString *)str otherButtonTitles:(NSArray *)titles attachTitle:(NSString *)attachTitle
{
    self = [super init];
    if (self) {
        _attachTitle = attachTitle;
        _cancelStr = str;
        _titles = titles;
        [self createUI];
    }
    return self;
}

/**
 修改某一项的titleColor

 @param color color 颜色
 @param index index 下标
 */
- (void)changeTitleColor:(UIColor *)color andIndex:(NSInteger )index{
    
    UIButton *btn = (UIButton *)[_buttomView viewWithTag:index + 10];
    [btn setTitleColor:color forState:UIControlStateNormal];
    
}

/**
 *  显示ActionSheet
 */
- (void)showActionSheet
{
    [self showActionSheetWithClearDismiss:YES];
}

- (void)showActionSheetWithClearDismiss:(BOOL)isClearDismiss {
    
    self.clearDismiss = isClearDismiss;
    typeof(self) __weak weak = self;
    [UIView animateWithDuration:0.3f delay:0 usingSpringWithDamping:0.8f initialSpringVelocity:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        [weak setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f]];
        [_buttomView setFrame:CGRectMake(0, kScreenHeight - _actionSheetHeight, kScreenWidth, _actionSheetHeight)];
        
    } completion:^(BOOL finished) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:weak action:@selector(dismiss:)];
        tap.delegate = self;
        [weak addGestureRecognizer:tap];
        
        [_buttomView setFrame:CGRectMake(0, kScreenHeight - _actionSheetHeight, kScreenWidth, _actionSheetHeight)];
    }];
}

#pragma mark -- private
- (void)createUI
{
    /*self*/
    [self setFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self setBackgroundColor:[UIColor clearColor]];
   
    
    /*buttomView*/
    UIView *buttomView = [[UIView alloc] init];
    
    buttomView.backgroundColor = kColorD9D9D9;
    
    _actionSheetHeight = _titles.count * (kSheetCellHeight+0.5f);
    if (_attachTitle) {
        _actionSheetHeight += kSubTitleHeight;
    }
    if (_cancelStr) {
        _actionSheetHeight += ((kSheetCellHeight+0.5f)+kCellSpacing);
    }
    
    [buttomView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, _actionSheetHeight)];
    _buttomView = buttomView;
    [self addSubview:_buttomView];
   
    
    /*CanceBtn*/
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancleBtn setBackgroundColor:[UIColor whiteColor]];
    [cancleBtn setFrame:CGRectMake(0, CGRectGetHeight(buttomView.bounds) - kSheetCellHeight, kScreenWidth, kSheetCellHeight)];
    [cancleBtn setTitleColor:kColor3C3C3C forState:UIControlStateNormal];
    [cancleBtn setTitle:_cancelStr forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(selectedButtons:) forControlEvents:UIControlEventTouchUpInside];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [cancleBtn setTag:10];
    if (_cancelStr) {
        [cancleBtn setFrame:CGRectMake(0, CGRectGetHeight(buttomView.bounds) - kSheetCellHeight, kScreenWidth, kSheetCellHeight)];
        [_buttomView addSubview:cancleBtn];
    }

    
    
    
    /*设置Items  文字大小颜色规格*/
    for (NSString *title in _titles) {
        
        NSInteger index = [_titles indexOfObject:title];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setBackgroundColor:[UIColor whiteColor]];
        
        CGFloat hei = 0;
        CGFloat y = 0;
        
        if (_cancelStr) {
            hei = (55.5 * _titles.count) + kCellSpacing;
            y = (CGRectGetMinY(cancleBtn.frame) + (index * (kSheetCellHeight+0.5))) - hei;
        } else {
            hei = (55.5 * (_titles.count - 1));
            y = (CGRectGetMinY(cancleBtn.frame) + (index * (kSheetCellHeight+0.5))) - hei;
        }
        
        [btn setFrame:CGRectMake(0, y, kScreenWidth, kSheetCellHeight)];
        [btn setTag:(index + 10)+1];
        [btn setTitleColor:kColor3C3C3C forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(selectedButtons:) forControlEvents:UIControlEventTouchUpInside];
        [_buttomView addSubview:btn];
    }

    
    //设置标题文字
    if (_attachTitle) {
        
        UILabel *attachTitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSubTitleHeight)];
        attachTitleView.backgroundColor = [UIColor whiteColor];
        attachTitleView.font = [UIFont systemFontOfSize:14.0f];
        attachTitleView.textColor = [UIColor grayColor];
        attachTitleView.text = _attachTitle;
        attachTitleView.textAlignment = 1;
        attachTitleView.numberOfLines = 0;
        
        
        [_buttomView addSubview:attachTitleView];
        
    }
}

/**
 *  按钮的点击事件
 *
 *  @param btns 被电击的按钮
 */
- (void)selectedButtons:(UIButton *)btns{
    
    typeof(self) __weak weak = self;
    [self dismissBlock:^(BOOL complete) {
        if (weak.selectButtonAtIndex) {
            weak.selectButtonAtIndex(btns.tag - 10);
        }
    }];
    
    
}



//隐藏
-(void)dismiss:(UITapGestureRecognizer *)tap{
    
    if( CGRectContainsPoint(self.frame, [tap locationInView:_buttomView])) {
        NSLog(@"tap");
    } else{
        
        [self dismissBlock:^(BOOL complete) {
            
        }];
    }
}

//隐藏ActionSheet的Block
-(void)dismissBlock:(completeAnimationBlock)block{
    
    
    typeof(self) __weak weak = self;
    CGFloat height = ((kSheetCellHeight+0.5f)+kCellSpacing) + (_titles.count * (kSheetCellHeight+0.5f)) + kCollectionViewHeight;
    
    [UIView animateWithDuration:0.3f delay:0 usingSpringWithDamping:0.8f initialSpringVelocity:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        
        UIColor *endColor = self.clearDismiss ? [UIColor clearColor] : self.backgroundColor;
        
        [weak setBackgroundColor:endColor];
        [_buttomView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, height)];
        
    } completion:^(BOOL finished) {
        
        block(finished);
        [self removeFromSuperview];
        
    }];
    
}



@end
